<template>
  <div class="private-msg">
    <h1>个人信息</h1>
    <el-form :model="userList" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="id" prop="uid">
        <el-input v-model="userList.uId" readonly></el-input>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="userList.uName"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-radio-group v-model="userList.uGender">
          <el-radio label="男" value="1"></el-radio>
          <el-radio label="女" value="0"></el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="年龄" required>
        <el-input v-model="userList.uAge"></el-input>
      </el-form-item>
      <el-form-item label="E-mail" prop="delivery">
        <el-input v-model="userList.uEmail"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">立即修改</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "PrivateMsg",
  data() {
    return {
      userList: {
        uId: '',
        uName: '',
        uAge: '',
        uAccount:'',
        uPassword:'',
        uGender: '',
        uEmail: ''
      },
      userForm:''
    };
  },
  methods:{
    submitForm(formName) {
      const _this=this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          axios.get("http://localhost:8089/user/updateUser",{params:_this.userList}).then(
              function (response) {
                if (response.data.code==0){
                  localStorage.setItem("user",JSON.stringify(response.data.data));
                  confirm("修改完成！")
                }else{
                  alert("修改失败!");
                }
              }
          )
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  created() {
    const _this=this
    _this.userForm=JSON.parse(localStorage.getItem("user"));
    _this.userList.uName=_this.userForm.uname
    _this.userList.uId=_this.userForm.uid
    _this.userList.uAccount=_this.userForm.uaccount
    _this.userList.uPassword=_this.userForm.upassword
    _this.userList.uAge=_this.userForm.uage
    _this.userList.uEmail=_this.userForm.uemail
    _this.userList.uGender=_this.userForm.ugender==='1'?'男':'女';
  }
}
</script>

<style scoped>
.private-msg{
  width: 50%;
  margin: 0 auto;
}
</style>